import os
import cv2
import read_tsp as rt
import math

folder_path = './path/'  

# 遍历文件夹
for filename in os.listdir(folder_path):
    img = cv2.imread('./image/'+filename[:-3]+'jpg')
    cities = rt.read_tsp_file('./data/%s'%filename[:-6]+'.tsp')
    distance = 0
    with open (folder_path+filename,'r') as f:
        lines = f.readlines()
        for i in range(7,len(lines)-1):
            city0 = int(lines[i]) - 1
            city1 = int(lines[i+1]) - 1
            distance += math.sqrt((cities[city0][0]-cities[city1][0])**2 + (cities[city0][1]-cities[city1][1])**2)
    x = 40  # 起始位置的x坐标
    y = 435  # 起始位置的y坐标
    width = 350  # 区域的宽度
    height = 100  # 区域的高度
    img[y:y+height, x:x+width] = (255, 255, 255)  # 设置为白色 
    len_text = 'length:' + str(round(distance,2)) + 'm'
    font = cv2.FONT_HERSHEY_SIMPLEX
    len_position = (50, 460)
    font_scale = 1
    color = (0, 0, 0)
    thickness = 1  
    cv2.putText(img, len_text, len_position, font, font_scale, color, thickness)
    #保存图像
    img_name = './image/'+filename[:-3]+'jpg'
    cv2.imwrite(img_name, img)
    cv2.imshow("result",img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()